package javango.db;

public abstract class AbstractPage<T> implements Page<T> {

	protected final Paginator<T> paginator;
	protected final int number;

	public AbstractPage(int number, Paginator<T> paginator) {
		this.number = number;
		this.paginator = paginator;
	}

	public String toString() {
		return String.format("<Page %s of %s>", this.getNumber(), this.paginator.getNumPages());
	}

	public boolean getHasNext() {
		return this.getNumber() < this.paginator.getNumPages();
	}

	public boolean getHasPrevious() {
		return this.getNumber() > 1;
	}

	public int getNumber() {
		return number;
	}

	public Paginator<T> getPaginator() {
		return paginator;
	}

	public int getPreviousPageNumber() {
		return this.number - 1;			
	}

	public int getNextPageNumber() {
		return this.number + 1;
	}

	public int getStartIndex() {
		if (this.paginator.getCount() == 0) {
			return 0;
		}
		return (this.paginator.getPerPage() * (this.number -1)) + 1;
	}

	public int getEndIndex() {
		if (this.number == this.paginator.getNumPages()) {
			return this.paginator.getCount();
		}
		return this.number * this.paginator.getPerPage();
	}

}